// Manager的各种配置参数
import { stringToDate } from "@/generic/Utils";
import { DEFAULT_USER_PASSWORD } from "@/generic/ConfigParams";
import { Icon, Input, Checkbox, Button, Select } from 'antd';
import { EmailInput, mySelect } from "../components/CustomHTML";

export const DATA_SOURCE = { type: 'user/fetchUsers' }

export const TABLE_COLS = [
    {
        dataIndex: 'key', text: "序号", width: '', isHidden: true,
        canClick: { type: 'manager/updateCurrentPage', payload: { mark: 'detail' } },
    },
    {
        dataIndex: 'nickName', text: "昵称", width: '', 
        canClick: { type: 'manager/updateCurrentPage', payload: {mark: 'detail'} },
    },
    {
        dataIndex: 'isSuper', text: "管理", width: '',
        render: (isSuper, _) => isSuper ? <Icon type="crown" style={{ color: '#ffaa00', fontSize: '24px' }} /> : '',
    },
    {
        dataIndex: 'enable', text: "启用", width: '', isHidden: true,
        render: (isEnable, _) => isEnable
            ? <Icon type="check-circle" style={{ color: '#52c41a' }} />
            : <Icon type="close-circle" style={{ color: 'gray' }} />,
    },
    {
        dataIndex: 'createdAt', text: "创建时间", width: '', isHidden: true,
        render: (date, _) => <span>{stringToDate(date)}</span>,
    },
    {
        dataIndex: 'log', text: "日志", width: '', isHidden: true,
        render: (date, _) => <Icon type="solution" />,
        canClick: { type: 'manager/updateCurrentPage', payload: { mark: 'logging' } },
    },
]

export const HEADER_OPTS = {
    left: [
        {
            isGroup: false,
            text: '添加',
            className: 'headerBtns',
            args: { icon: 'plus', type: 'primary', },
            action: { type: 'manager/updateModalVisible', payload: true },
        },
        { 
            isGroup: false,  
            text: '删除', 
            className: 'headerBtns', 
            args: { icon: 'delete', type: 'danger', },
            isHidden: true,
            confirm: { 
                name: '用户', 
                action: { type: 'manager/methodAdapter', method: 'delete', payload: {} }, 
            },
            msgTip: '删除',
        },
        {
            isGroup: true, className: 'headerBtns', isHidden: true,
            groups: [
                { 
                    text: '启用', 
                    args: { icon: 'check', },
                    action: { type: 'manager/methodAdapter', method: 'update', payload: {enable: true}},
                    msgTip: '启用',
                },
                { 
                    text: '禁用', 
                    args: { icon: 'close', },
                    action: { type: 'manager/methodAdapter', method: 'update', payload: { enable: false } }, 
                    msgTip: '禁用',
                },
            ]
        },
    ],
    right: {
        onSearch: () => { },
    },
}

export const MODAL_CFGS = {
    // okText: '',  // 设置submit按钮文本
    // cancelText: '',  // 设置cance按钮文本
    title:'添加用户',
    defaultLabelCol: { span: 5 },
    defaultWrapperCol: { span: 15 },
    okAction: { type: 'manager/methodAdapter', method: 'add', payload: { } },
    onCancel: (dispatch) => dispatch({ type: 'manager/updateModalVisible', payload: false }),
    formItems: [
        {
            label: '昵称', name: 'nickName',
            options: {
                rules: [{ message: '请输入至少两个字符的名称！', min: 2, }],
            },
            html: <Input placeholder="请输入账号昵称" />,
        },
        {
            label: '账号', name: 'username',
            options: {
                rules: [{ required: true, message: '请输入至少两个字符的名称！', min: 2, }],
            },
            html: <Input placeholder="请输入账号名称" />,
        },
        {
            label: '密码', name: 'password',
            options: {
                initialValue: DEFAULT_USER_PASSWORD,
                rules: [{ required: true, message: '请输入至少8位密码！', min: 8, }],
            },
            html: <Input placeholder="请输入密码" />,
        },
        {
            label: '邮箱', name: 'email',
            html: <EmailInput />,
        },
        {
            name: 'enable', wrapperCol: { xs: { span: 15 }, md: { span: 15, offset: 5 }, lg: { span: 15, offset: 5 }, },
            options: {
                valuePropName: 'checked',
                initialValue: false,
            },
            html: <Checkbox>启用</Checkbox>,
        },
        {
            name: 'isSuper', wrapperCol: { xs: { span: 15 }, md: { span: 15, offset: 5 }, lg: { span: 15, offset: 5 }, },
            options: {
                valuePropName: 'checked',
                initialValue: false,
            },
            html: <Checkbox>管理员</Checkbox>,
        },
    ],
}

//  #### DETAIL_ARGS ####
const leave = { type: 'manager/updateCurrentPage', payload: null }
const clOptions = [
    { value: 0, text: '不涉密'},
    { value: 1, text: '秘密' },
    { value: 2, text: '保密' },
    { value: 3, text: '机密' },
    { value: 4, text: '绝密' },
]
const groupsOptions = [  // 后面应该是动态查询组
    { value: 1, text: '组1' },
    { value: 2, text: '组2' },
    { value: 3, text: '组3' },
    { value: 4, text: '组4' },
]
export const DETAIL_ARGS = {
    detailTitle: '用户详情',
    updateTitle: '用户修改',
    leave: leave,
    defaultLabelCol: { span: 5 },
    defaultWrapperCol: { span: 15 },
    defaultColSpan: { xs: 24, md: 12, lg: 12},
    content: {
        account: [
            { label: 'ID', index: 'id' },
            { label: '账号', index: 'username' },
            { 
                label: '管理员', index: 'isSuper', canUpdate: true, elem: <Checkbox />,
                options: {
                    valuePropName: 'checked',
                    initialValue: false,
                },
            },
            { label: '系统创建', index: 'isSys' },
            { label: '磁盘', index: 'homeDisk' },
            { 
                label: '启用', index: 'enable', canUpdate: true, elem: <Checkbox />,
                options: {
                    valuePropName: 'checked',
                    initialValue: false,
                },
            },
            { 
                label: '组', index: 'groups', canUpdate: true, elem: mySelect(groupsOptions),
            },
            { label: '权限', index: 'cl', canUpdate: true, elem: mySelect(clOptions)  },
            { label: '创建时间', index: 'createdAt', handle: (text) => stringToDate(text), },
        ],
        userInfo: [
            { label: '昵称', index: 'nickName', canUpdate: true, },
            { label: '真实姓名', index: 'trueName', canUpdate: true, },
            { label: '邮箱', index: 'email', canUpdate: true, elem: <EmailInput /> },
            { label: '手机号', index: 'mobile', canUpdate: true, },
            { label: '电话', index: 'tel', canUpdate: true, },
        ],
    },
    footer: [
        { 
            elem: Icon,
            style: { float: 'left', height: '32px', lineHeight: '32px' },
            args: {
                type: 'arrow-left',
            },
            onClick: (dispatch) => { dispatch(leave) },
            // html: <xxx />  // 复杂的直接返回组件
        },
        {
            mark: 'detail',
            elem: Button,
            style: { float: 'right', marginRight: '8px' },
            args: {
                // icon: 'edit',
                type: 'danger',
            },
            text: '删除',
            onClick: (dispatch) => { }
        },
        { 
            mark: 'detail',
            elem: Button,
            style: { float: 'right', marginRight: '8px' },
            args: {
                // icon: 'edit',
                type: 'primary',
            },
            text: '修改',
            onClick: (dispatch, record) => { 
                dispatch({ type: 'manager/updateCurrentPage', payload: { mark: 'update', record} })
            } 
        },
        {
            mark: 'update',
            elem: Button,
            style: { float: 'right', marginRight: '8px' },
            text: '取消',
            onClick: (dispatch) => { }
        },
        {
            mark: 'update',
            elem: Button,
            style: { float: 'right', marginRight: '8px' },
            args: {
                // icon: 'edit',
                type: 'primary',
                // htmlType="submit",
            },
            text: '保存',
            isValidate: true,
            onClick: (dispatch, record, updateFields) => {
                return dispatch({ type: 'manager/methodAdapter', method: 'update', payload: { value: updateFields, rows: [record] } })
            }
        },
    ],
}